package com.dykj.apps.web.sysmanager.action;

import java.util.Map;

import com.dykj.apps.web.core.BaseAction;
import com.dykj.apps.web.sysmanager.dto.SysUserDTO;
import com.dykj.apps.web.sysmanager.service.IUserManagerBS;
import com.dykj.platform.framework.exception.ApplicationException;

public class SysUserAction extends BaseAction {

	private static final long serialVersionUID = -8342825948054725707L;

	private String radio_id;

	private SysUserDTO userDTO = new SysUserDTO();

	private Map roleMap;

	private Map groupMap;

	private IUserManagerBS bs;
	
	private String password;

	/**
	 * 初始化添加用户
	 * 
	 * @return
	 */
	public String initAddUser() throws ApplicationException {
		Map info = (Map) this.getSession().getAttribute("userinfo");
		String username = info.get("username").toString();
		String type = info.get("type").toString();
		String groupid = info.get("groupid").toString();
		roleMap = bs.queryAllRole(username);
		groupMap = bs.getGroupMap(type, groupid);
		this.request("editable", "true");// 表单可编辑（默认）
		this.request("action", "userManager");// action名称
		this.request("bn_value", "新增");// 按钮名称
		this.request("bn_method", "saveUser;");// 按钮的方法
		return "forwardinitAdduser";
	}

	/**
	 * 保存用户
	 * 
	 * @return
	 */
	public String saveUser() throws ApplicationException {
		if (userDTO.getUserid() == null) {
			alert("用户ID不能为空！");
			return initAddUser();
		} else if (null != bs.getOneSysUser(userDTO.getUserid())) {
			alert("用户ID重复!");
			return initAddUser();
		} else {
			Map info = (Map) this.getSession().getAttribute("userinfo");
			String username = info.get("username").toString();
			userDTO.setCreator(username);
			bs.saveOrUpdateUser(userDTO);
			userDTO.init();
		}
		alert("保存成功！"); // 提示信息
		return queryForPage();
	}

	/**
	 * 查看用户信息
	 * 
	 * @return
	 */
	public String viewUser() throws ApplicationException {
		SysUserDTO userdto = bs.getOneSysUser(radio_id);
		// 设置查询条件
		userdto.setP_dept(userDTO.getP_dept());
		userdto.setP_isleader(userDTO.getP_isleader());
		userdto.setP_userid(userDTO.getP_userid());
		userDTO = userdto;
		Map info = (Map) this.getSession().getAttribute("userinfo");
		String username = info.get("username").toString();
		String type = info.get("type").toString();
		String groupid = info.get("groupid").toString();
		roleMap = bs.queryAllRole(username);
		groupMap = bs.getGroupMap(type, groupid);
		this.request("editable", "false");// 表单是不可编辑（默认）
		this.request("action", "userManager");// action名称
		this.request("bn_value", "修改;返回");// 按钮名称
		this.request("bn_method", "initUpdateUser;queryForPage");// 按钮的方法
		return "forwardinitViewUser";
	}

	/**
	 * 初始化修改用户
	 * 
	 * @return
	 */
	public String initUpdateUser() throws ApplicationException {
		Map info = (Map) this.getSession().getAttribute("userinfo");
		String username = info.get("username").toString();
		String type = info.get("type").toString();
		String groupid = info.get("groupid").toString();
		SysUserDTO userdto = bs.getOneSysUser(radio_id);
		// 设置查询条件
		userdto.setP_dept(userDTO.getP_dept());
		userdto.setP_isleader(userDTO.getP_isleader());
		userdto.setP_userid(userDTO.getP_userid());
		userDTO = userdto;
		roleMap = bs.queryAllRole(username);
		groupMap = bs.getGroupMap(type, groupid);
		this.request("editable", "true");// 表单可编辑（默认）
		this.request("action", "userManager");// action名称
		this.request("bn_value", "修改;返回");// 按钮名称
		this.request("bn_method", "updateUser;queryForPage");// 按钮的方法
		return "forwardinitViewUser";
	}

	/**
	 * 保存修改用户
	 * 
	 * @return
	 */
	public String updateUser() throws ApplicationException {
		if (userDTO.getUserid() == null) {
			alert("用户ID不能为空！");
			return initUpdateUser();
		} else {
			Map info = (Map) this.getSession().getAttribute("userinfo");
			String username = info.get("username").toString();
			String type = info.get("type").toString();
			String groupid = info.get("groupid").toString();
			userDTO.setCreator(username);
			bs.saveOrUpdateUser(userDTO);
			roleMap = bs.queryAllRole(username);
			groupMap = bs.getGroupMap(type, groupid);
			alert("保存成功！");
			this.request("editable", "true");// 表单可编辑（默认）
			this.request("action", "userManager");// action名称
			this.request("bn_value", "修改;返回");// 按钮名称
			this.request("bn_method", "updateUser;queryForPage");// 按钮的方法
			return viewUser();
		}
	}

	/**
	 * 删除用户
	 * 
	 * @return
	 */
	public String deleteUser() throws ApplicationException {
		bs.delUser(radio_id);
		alert("删除成功！");
		return queryForPage();
	}

	/**
	 * 初始化分页信息
	 * 
	 * @return
	 */
	public String initQueryForPage() throws ApplicationException {
		this.completeResultList(new SysUserDTO());
		this.request("action", "userManager");// action名称
		this.request("bn_value", "添加新用户");//按钮名称
		this.request("bn_method", "initAddUser");//按钮的方法
		return "forwardUserManager";
	}

	/**
	 * 初始化分页信息
	 * 
	 * @return
	 */
	public String queryForPage() throws ApplicationException {
		roleMap = bs.queryAllRole(null);
		groupMap = bs.getGroupMap("0", null);
		Map info = (Map) this.getSession().getAttribute("userinfo");
		String groupid = info.get("groupid").toString();
		String type = info.get("type").toString();
		userDTO.setGroupid(groupid);
		userDTO.setType(type);
		this.setPageList(bs.querySysUser(userDTO, this.getPage())); // 把查询的结果放入LIST
		this.completeResultList(new SysUserDTO());// 条数不够补齐
		this.request("pageMethod", "queryForPage");
		this.request("action", "userManager");// action名称
		this.request("bn_value", "查看;修改;删除;添加新用户");// 按钮名称
		this.request("bn_method",
				"viewUser;initUpdateUser;deleteUser;initAddUser");// 按钮的方法
		this.request("bn_check", "true;true;true;false");// 提交时是否验证单选按钮
		return "forwardUserManager";
	}

	/*
	 * ****************************自我信息管理*********************************
	 */
	/**
	 * 个人密码修改初始化
	 * 
	 * @return
	 */
	public String viewMe() throws ApplicationException {

		this.request("editable", "true");// 表单是不可编辑（默认）
		this.request("action", "userManager");// action名称
		this.request("bn_value", "确认;返回");// 按钮名称
		this.request("bn_method", "editMe;returnIndex");// 按钮的方法
		return "viewMe";
	}

	/**
	 * 修改个人密码
	 * 
	 * @return
	 */
	public String editMe() throws ApplicationException {
		Map info = (Map) this.getSession().getAttribute("userinfo");
		userDTO = bs.getOneSysUser(info.get("username").toString());
		if(userDTO.getPasswd()!=password.trim()&&!userDTO.getPasswd().equals(password.trim())){
			alert("您输入的密码不正确，请重试！");
			password="";
			return viewMe();
		}
		userDTO.setPasswd("");
		this.request("editable", "true");// 表单是不可编辑（默认）
		this.request("action", "userManager");// action名称
		this.request("bn_value", "保存");// 按钮名称
		this.request("bn_method", "updateMe");// 按钮的方法
		return "updateMe";
	}
	
	/**
	 * 更新个人密码
	 * @return
	 * @throws ApplicationException 
	 */
	public String updateMe() throws ApplicationException{
		if(bs.saveOrUpdateUser(userDTO)==0){
			alert("祝贺您！密码修改成功!");
		}
		this.request("editable", "true");// 表单是不可编辑（默认）
		this.request("action", "userManager");// action名称
		this.request("bn_value", "确认;返回");// 按钮名称
		this.request("bn_method", "editMe;returnIndex");// 按钮的方法
		return "viewMe";	
	}
	
	public String returnIndex() throws ApplicationException{
		return "returnIndex";	
	}

	/*
	 * ================================get
	 * set方法=====================================
	 */
	public IUserManagerBS getBs() {
		return bs;
	}

	public void setBs(IUserManagerBS bs) {
		this.bs = bs;
	}

	public String getRadio_id() {
		return radio_id;
	}

	public void setRadio_id(String radio_id) {
		this.radio_id = radio_id;
	}

	public Object getModel() {
		return userDTO;
	}

	public Map getRoleMap() {
		return roleMap;
	}

	public void setRoleMap(Map roleMap) {
		this.roleMap = roleMap;
	}

	public SysUserDTO getUserDTO() {
		return userDTO;
	}

	public void setUserDTO(SysUserDTO userDTO) {
		this.userDTO = userDTO;
	}

	public Map getGroupMap() {
		return groupMap;
	}

	public void setGroupMap(Map groupMap) {
		this.groupMap = groupMap;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

}
